//index.js
//获取应用实例
const app = getApp()
var amapFile = require('../../libs/amap-wx.js');
var markersData = {
  latitude: '',//纬度
  longitude: '',//经度
  key: "c36596e1886e08b03ffa4be8b302e1c7"//申请的高德地图key
};
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    temp:"4",
    temperature:"-1/10℃",
    type:"晴",
    city:"成都",
    date:"2000-01-01",
    direction:"无持续风向微风级",
    condition:"10",
    futureArray:"[]",


  },
  
  //事件处理函数
  bindViewTab: function() {
    wx.navigateTo({
      url: '../test1/test1'
    })
  },
  switchBtn: function() {
    wx.switchTab({
      url: '../main/main',
      success: function() {

      },
      fail: function() {

      },
      complete () {

      }
    })
  },
  onLoad: function () {
    // wx.request({
    //   url: 'http://apis.juhe.cn/simpleWeather/query?',
    //   data: {
    //     "city": '成都',
    //     "key": '827a4100771d122f3a0c2e2df51c5999'
    //   },
    //   success: res => {
    //     var result = res;
    //     var test1 = JSON.stringify(result);
    //     console.log(res.data)
    //     this.setData({
    //       city: res.data.result.city,
    //       temp: res.data.result.realtime.temperature,
    //       temperature: res.data.result.future[0].temperature,
    //       type: res.data.result.realtime.info,
    //       direction: res.data.result.realtime.direct,
    //       date: res.data.result.future[0].date,
    //     })
    //   }
    // })
    this.loadInfo();
  },
  loadInfo: function(){
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude//维度
        var longitude = res.longitude//经度
        console.log(res);
        that.loadCity(latitude, longitude);
      }
    })
  },
  loadCity: function (latitude, longitude) {
    var that = this;
    var myAmapFun = new amapFile.AMapWX({ 
      key: markersData.key 
      });
    myAmapFun.getRegeo({
      location: '' + longitude + ',' + latitude + '',//location的格式为'经度,纬度'
      success: function (data) {
        console.log("5645" + data);
      },
      fail: function (info) { }
    });

    myAmapFun.getWeather({
      success: function (data) {
     that.setData({
          city: data.city.data,
          temp: data.liveData.temperature,
          date: data.liveData.reporttime,
          type: data.liveData.weather,
          temperature: data.winddirection.data
        })
        
        console.log(data);
        //成功回调
      },
      fail: function (info) {
        //失败回调
        console.log(info)
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})

